package com.cms.util;

import java.util.HashMap;
import java.util.Map;

import com.jfinal.plugin.activerecord.Db;

public class FieldUtils {
    
    public static Map<String,String> fieldTypeMap = new HashMap(){{
        put("input", "varchar(255)");
        put("textarea", "text");
        put("editor", "longtext");
        put("radio", "varchar(255)");
        put("checkbox", "varchar(255)");
        put("file", "varchar(255)");
        put("multipleFile", "text");
        put("select", "varchar(255)");
        put("date", "datetime");
    }};
    
    public static void add(String table,String fieldName,String fieldType){
        StringBuilder sql = new StringBuilder();
        sql.append("alter table "+table+" add "+fieldName+" "+fieldTypeMap.get(fieldType)+" DEFAULT NULL;");
        Db.update(sql.toString());
    }
    
    public static void update(String table,String oldFieldName,String newFieldName,String newFieldType){
        StringBuilder sql = new StringBuilder();
        sql.append("alter table "+table+" change "+oldFieldName+" "+newFieldName+" "+fieldTypeMap.get(newFieldType)+" DEFAULT NULL;");
        Db.update(sql.toString());
    }
    
    public static void delete(String table,String fieldName){
        StringBuilder sql = new StringBuilder();
        sql.append("alter table "+table+" drop column "+fieldName+";");
        Db.update(sql.toString());
    }

}
